/**
 * 
 */
package com.ckex.learn.lucene.main;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.lucene.search.BooleanClause;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ckex.learn.lucene.dao.*;
import com.ckex.learn.lucene.dao.page.*;
import com.ckex.learn.lucene.domain.TradeDo;
import com.ckex.learn.lucene.index.IndexService;
import com.ckex.learn.lucene.search.SearchService;

/**
 * @author ckex created 2014-1-17 下午2:35:35
 * @explain -
 */
public class Main {

    protected static Logger          logger = Logger.getLogger(Main.class);

    public static ApplicationContext ctx    = null;

    public static void main(String[] args) {
        ctx = new ClassPathXmlApplicationContext("classpath*:spring/*.xml");
        search();
    }

    protected static void search() {

        String field = "receiver_city";
        String value = "上海";
        TradeDao tradeDao = (TradeDao) ctx.getBean("tradeDao");

        List<Long> ids = SearchService.search(field, value);

        for (Long id : ids) {
            TradeDo tradeDo = tradeDao.loadById(null, id);
            //            logger.info(tradeDo.toString());
            logger.info(tradeDo.getBuyerNick() + " \t " + tradeDo.getReceiverCity());
        }

        logger.info("\r\n");
        logger.info("-----------------------------------------------------");
        logger.info("\r\n");

        int a = ids.size();

        ids = SearchService.simpleSearch(field, value);

        for (Long id : ids) {
            TradeDo tradeDo = tradeDao.loadById(null, id);
            //            logger.info(tradeDo.toString());
            logger.info(tradeDo.getBuyerNick() + " \t " + tradeDo.getReceiverCity());
        }

        logger.info("\r\n");
        logger.info("-----------------------------------------------------");
        logger.info("\r\n");

        String fields[] = { "nick", "receiver_city" };
        String values[] = { "23807614", "上海" };
        //MUST  必须
        //MUST_NOT 否定
        //SHOULD 匹配任意一个field
        BooleanClause.Occur[] clauses = { BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT };

        int b = ids.size();

        ids = SearchService.search(fields, values, clauses);

        for (Long id : ids) {
            TradeDo tradeDo = tradeDao.loadById(null, id);
            //            logger.info(tradeDo.toString());
            logger.info(tradeDo.getBuyerNick() + " \t " + tradeDo.getReceiverCity());
        }

        logger.info(" ======== >   \r\n" + a + " \t " + b + " \t " + ids.size());
    }

    protected static void index() {

        TradeDao tradeDao = (TradeDao) ctx.getBean("tradeDao");

        Long sellerId = 1374570578029l;
        PageQuery pageQuery = new PageQuery(1, 100);
        Integer count = null;
        boolean create = Boolean.TRUE;
        boolean next = Boolean.FALSE;
        while (next) {
            long t3 = System.currentTimeMillis();
            PageList<TradeDo> result = tradeDao.listByPage(sellerId, pageQuery, count);

            if (result == null || result.isEmpty()) {
                break;
            }
            long t4 = (System.currentTimeMillis() - t3);

            for (TradeDo tradeDo : result) {
                IndexService.createIndexFile(tradeDo, create);
                create = Boolean.FALSE;
            }

            long t5 = System.currentTimeMillis();

            logger.info(String.format("{load time :(%s), 处理 (%s) 条耗时:(%s) }", t4, result.size(),
                    (t5 - (System.currentTimeMillis()) - t4)));

            Paginator paginator = result.getPaginator();
            count = paginator.getItems();
            if (paginator.getNextPage() == paginator.getPage()) {
                count = paginator.getItems();
                break;
            }
            if ((paginator.getLastPage() - paginator.getPage()) <= 2) {
                count = null;
            }
            pageQuery.increasePageNum();
        }
    }
}
